<?php
// calendar page
require_once WACT_ROOT.'controller/controller.inc.php';
require_once WACT_ROOT.'controller/form.inc.php';
require_once WACT_ROOT. 'util/dataspace.inc.php';
require_once APP_ROOT.'view/common/layout.formview.php';

require_once 'view/pub/calendar/calendar.index.php';
require_once 'view/pub/calendar/event.view.php';
require_once 'model/common/session.php';
require_once 'model/common/event.php';
require_once 'model/common/user.php';
require_once APP_ROOT . "model/common/site.php";

class CalendarIndexPage extends PageController {

	var $form;
	var $record;

    function CalendarIndexPage() {
        parent::PageController();

        $Form=new FormController();
		$Form->addChild('go',new ButtonController(new Delegate($this, 'go')));
		$Form->setDefaultChild('go');
		$this->addChild('CalendarForm',$Form);
		$this->setDefaultChild('CalendarForm');


        $mytemplate = 'pub/calendar/calendar.html';
        $mytemplate2 = 'pub/calendar/event.html';
        $mytemplate3 = 'pub/calendar/event_teaser.html';

        $this->setDefaultView(new Handle('CalendarView', array($mytemplate)));

        $this->registerOnActivateListener(new Delegate($this, 'guard'));
        $Form->registerOnLoadListener(new Delegate($this, 'load'));

        $this->addView('success', WACT_DEFAULT_VIEW);
        $this->addView('notfound', WACT_DEFAULT_VIEW);
        $this->addView('view', new Handle('EventView', array($mytemplate2)));
        $this->addView('teaser', new Handle('EventView', array($mytemplate3))); ;
        $this->addView('delete', new Handle('RedirectView', array('/delete')));
        $this->addView('this', WACT_DEFAULT_VIEW);
        $this->addView('index', new Handle('RedirectView', array('/')));
        $this->addView('login', new Handle('RedirectView', array('/login')));
    }

    function isSubscriber() {
        // only allow subscribers to view
        $is_subscriber = false;
        $logged_in = Session::isLogin();
    	$user_id = 0;
    	if ($logged_in) $user_id = Session::getUserId();
    	if ($user_id) $is_subscriber = User::isUserSubscriptionActive($user_id);
    	return $is_subscriber;
    }

    function isAdmin() {
        // only allow subscribers to view
        $is_admin = false;
        $logged_in = Session::isLogin();
    	$user_id = 0;
    	if ($logged_in) $user_id = Session::getUserId();
    	if ($user_id) $is_admin = User::isUserAdmin($user_id);
    	return $is_admin;
    }

    function isPremium($pending_event_id) {
    	if (!$pending_event_id) return false;
        if (is_null( $this->record )) $this->record =& PendingEvent::getRecord($pending_event_id);
        //print_r( $this->record );
        if (is_null( $this->record )) return false;
        $premium = $this->record->get('premium');
        //print 'Premium '.$premium;
        return $premium?true:false;
    }

    function canView($pending_event_id) {
    	//print 'Can view '.$pending_event_id;
    	$premium = $this->isPremium($pending_event_id);
    	if (!$premium) return true; // everybody can view normal content
        // only allow subscribers to view
        $is_subscriber = $this->isSubscriber();
        if ($is_subscriber) return $is_subscriber;
        $is_admin = $this->isAdmin();
        return $is_admin;

    }

    function guard(&$source, &$request, &$responseModel) {

        if ($request->hasParameter('pending_event_id')) {
            $pending_event_id = $request->getParameter('pending_event_id');
        	// only allow subscribers to view
        	$canView = $this->canView($pending_event_id);
            if ($canView) return 'view';
            else return 'teaser';
        }
        //return 'success';
    }

    function load(&$source, &$request, &$responseModel) {
        if ($request->hasParameter('pending_event_id')) {
           $pending_event_id = $request->getParameter('pending_event_id');

        }
        //print_r(  $responseModel );
        //print 'Loading page...';

        if (!$pending_event_id) {
    		$month = $responseModel->get('month');
        	$year = $responseModel->get('year');

        	if (!$month) $month = $request->getParameter('month');
        	if (!$year) $year = $request->getParameter('year');

        	if (!$month) $month = (integer)date('m');
        	if (!$year) $year = (integer)date('Y');

        	$responseModel->set('month',$month);
        	$responseModel->set('year',$year);

        }  else {
            // only allow subscribers to view
        	$canView = $this->canView($pending_event_id);
            if ($canView) return 'view';
            else return 'teaser';
        }

    	return 'success';
    }

}

?>